<?php

namespace WXAPI\Controller;

use Think\Log;

class LiveController extends BaseController
{

    public function live_list()
    {
        $store_id = I('store_id');
        $cPage = I('cPage');

        $cPage = $cPage * 30;
        getWechatLive($store_id);
        $where = "dataflag = 1 and store_id=".$store_id;
        $list = M('live')->where($where)->order('roomid desc')->limit($cPage,15)->select();
        foreach ($list as $key=>$item){
//            $list[$key]['live_status'] = C('WECHAT_LIVE_STATUS')[$item['live_status']];
            $list[$key]['goods'] =  json_decode($item['goods'],true);
        }
        exit(jsonReturn(1,'获取成功',$list));
    }


    public function video_list()
    {
        $store_id = I('store_id');
        $cPage = I('cPage');

        $cPage = $cPage * 30;
        $where = "dataflag = 1 and store_id=".$store_id;
        $list = M('video')->where($where)->order('sort desc')->limit($cPage,15)->select();
        $store = M('store')->where(['store_id'=>$store_id])->find();
        foreach ($list as $key=>$item){
//            $list[$key]['live_status'] = C('WECHAT_LIVE_STATUS')[$item['live_status']];
            $list[$key]['share_img'] = C('MYOSS')['url'].$item['share_img'];
            $list[$key]['finder_user'] = $store['finder_user'];
        }
        exit(jsonReturn(1,'获取成功',$list));
    }

    public function video_info()
    {
        $id = I('id');
        $store_id = I('store_id');
        $info = M('video')->where(['id'=>$id,'dataflag'=>1])->find();
        $store = M('store')->where(['store_id'=>$store_id])->find();
        if($info){
            $info['share_img'] = C('MYOSS')['url'].$info['share_img'];
            $info['finder_user'] = $store['finder_user'];
        }
        exit(jsonReturn(1,'获取成功',$info));
    }


    public function set_live_channels()
    {
        $data = file_get_contents('php://input');
        $data = json_decode($data,true);
        $store_id = $data['store_id'];
        $live_json = json_decode($data['live_json'],true);
        Log::record($data,'DEBUG');
        if($data){
            foreach ($live_json as $item){
                $channels = M('live_channels')->where(['nonceId'=>$item['nonceId']])->find();
                $attr = [
                    'description'=>$item['description'],
                    'headUrl'=>$item['headUrl'],
                    'nickname'=>$item['nickname'],
                    'playbackState'=>$item['playbackState'],
                    'replayStatus'=>$item['replayStatus'],
                    'status'=>$item['status'],
                    'store_id'=>$store_id,
                    'update_time'=>date('Y-m-d H:i:s',time()),
                ];
                $attr_add = [
                    'add_time'=>date('Y-m-d H:i:s',time()),
                    'feedId'=>$item['feedId'],
                    'nonceId'=>$item['nonceId']];
                if($channels){
                    M('live_channels')->where(['id'=>$channels['id']])->setField($attr);
                }else{
                    $data_add = array_merge($attr,$attr_add);
                    M('live_channels')->add($data_add);
                }
            }

            exit(jsonReturn(1,'上传直播成功',[]));
        }
    }

    public function live_channels_list()
    {
        $store_id = I('store_id');
        $cPage = I('cPage');

        $cPage = $cPage * 30;
        $where = "is_show = 1 and store_id=".$store_id;
        $list = M('live_channels')->where($where)->order('sort asc,id desc')->limit($cPage,15)->select();
        foreach ($list as $key=>$item){
            $list[$key]['live_status'] = C('WECHAT_CHANNELS_LIVE_STATUS')[$item['status']];
        }
        exit(jsonReturn(1,'获取成功',$list));
    }


    public function set_live_notice()
    {
        $data = file_get_contents('php://input');

        $data = json_decode($data,true);

        $notice_id = $data['notice_id'];
        $user_id = $data['user_id'];
        $start_time = date('Y-m-d H:i',$data['start_time']);
        M('live_notice')->add(['notice_id'=>$notice_id,'user_id'=>$user_id,'start_time'=>$start_time]);

        exit(jsonReturn(1,'预约成功',[]));
    }

    public function get_live_notice()
    {
        $data = file_get_contents('php://input');

        $data = json_decode($data,true);

        $notice_id = $data['notice_id'];
        $user_id = $data['user_id'];
        $live_notice = M('live_notice')->where(['notice_id'=>$notice_id,'user_id'=>$user_id])->find();
        $live_notice = $live_notice ? 1 : 0;
        exit(jsonReturn(1,'预约状态',['data'=>$live_notice]));
    }
}